<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $uses = array('Role','Permission','UserRole');
    function beforeFilter()
    {       
        parent::beforeFilter();
        if(in_array($this->params['action'],array('signup','login')) && $this->Session->read('User.username')!='')
        {
            $this->redirect('/');
        }    
    }
    
	function signup()
	{	   
		if(!empty($this->data))
		{		  
			if($data = $this->User->save($this->data))
			{
				$this->email->sendEmail('users_welcome_message',$data);
				$this->email->sendEmail('users_email_verification',$data);
			}
		}
        
        $this->set('roles',$this->Role->getDisplayRoles());
	}
	
	function login()
	{	   
	   if(!empty($this->data))
       {             
           if($data = $this->User->login($this->settings['login_method'],$this->data['User']['username'],$this->data['User']['password']))
           {            
              $this->Session->write('User.username',$data['User']['username']);
              $this->Session->write('User.first_name',$data['User']['first_name']); 
              $this->Session->write('User.last_name',$data['User']['last_name']);
              $this->Session->write('User.permissions',$data['permissions']);              
              $this->redirect('/');
           }    
           else
           {
             $this->Session->setFlash(__('Username/password may be wrong.',true),true);
             $this->redirect('/users/login');
             exit(0);
           }
       }
	}
    
    function logout()
    {
        $this->Session->destroy();
        $this->redirect('/');
    }
	
	function forgotPassword()
	{
		
	}
	
	function settings()
	{
	   $this->check('change_password');
	}
    
    function index()
    {
        
    }
}
